package top.jacktgq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.web.bind.annotation.*;
import top.jacktgq.pojo.Book;
import top.jacktgq.service.IBookService;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author CandyWall
 * @Date 2022/1/23--23:25
 * @Description
 */
@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private IBookService bookService;
    /*@GetMapping("/{id}")
    public String getById(@PathVariable int id) {
        System.out.println("id = " + id);
        return "getById...";
    }*/

    @GetMapping("/{id}")
    public Book getById(@PathVariable int id) {
        System.out.println("id = " + id);
        Book book = new Book();
        book.setId(5);
        book.setName("神秘岛");
        book.setType("科幻");
        book.setDescription("《神秘岛》是法国科幻小说家儒勒·凡尔纳创作的长篇小说，是他写的三部曲之一。");

        return book;
    }
    @PostMapping("/save")
    public Book save(@RequestBody Book body) {
        System.out.println(bookService.getClass().getName());
        System.out.println(bookService.toString());
        Book book = new Book();
        book.setName("springboot");
        book.setType("springboot");
        book.setDescription("springboot");
        bookService.save(book);
        return book;
    }
    @PostMapping("/saveBatch")
    public Book saveBatch(@RequestBody Book body) {
//        MethodInterceptor
        System.out.println(bookService.getClass().getName());
        System.out.println(bookService.toString());
        List<Book> list=new ArrayList<>();
        Book book = new Book();
        book.setName("springboot");
        book.setType("springboot");
        book.setDescription("springboot");
        list.add(book);
        book = new Book();
        book.setName("张三");
//        book.setName("springbootspringbootspringbootspringbootspringbootspringbootspringbootspringbootspringbootspringbootspringbootspringbootspringbootspringbootspringbootspringbootspringbootspringboot");
        book.setType("springboot");
        book.setDescription("springboot");
        list.add(book);
        bookService.saveBatch(list);
        return book;
    }
    @GetMapping
    public List<Book> getAll() {
        return bookService.list();
    }
}
